﻿using Microsoft.EntityFrameworkCore;
using PublicModelsAndEntities.Models.MainsortRecord;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using XinjeMesProgram.Entities.BookInfo;

namespace XinjeMesProgram.Entities.Function
{
    public class XinjeBookingInfo
    {
        public static List<MainSortBookModel> GetMainSortBook(string Code)
        {
            List<MainSortBookModel> mainSortBooks = new List<MainSortBookModel>();

            if (!string.IsNullOrEmpty(Code))
            {
                using (FactoryMesContext factoryMes = new FactoryMesContext())
                {
                    string SQL = string.Format("select a.BookingBarNo,a.BookingTime,a.BookingType,a.BookingName,b.BookingGuest,b.BookingSale,b.BookingToPlace,b.BookingCode  from BookingInfo a ,BookingListInfo b  where a.BookingListNo = b.BookingListNo and a.BookingType = b.BookingType and a.BookingBarNo='{0}'", Code);
                    // string SQL_1 = string.Format("select a.productionType,a.productionName  from Productinfo a  where  a.Productinfo='{0}'", Code);  //   and  c.productionType='80545'   , Productinfo c   ,c.productionName
                    mainSortBooks = factoryMes.MainSortBooks.FromSqlRaw(SQL).ToList();
                    Console.WriteLine(SQL);
                }
            }
            return mainSortBooks;
        }

        // 产品型号查询
        public static List<GetProductType> GetProduct(string Code)
        {
            int startIndex = Code.Length - 5;
            int length = 5;
            string result = Code.Substring(startIndex, length);

            List<GetProductType> getProductType = new List<GetProductType>();
            StringBuilder csvContent = new StringBuilder();
            if (!string.IsNullOrEmpty(Code))
            {
                using (FactoryMesContext factoryMes = new FactoryMesContext())
                {
                    string SQL = string.Format("select a.MnemonicCode,a.Code,a.Name,a.ProductCategory,a.SpecificationAndModel,a.ClassifICation,a.MaterialProperties  from ProductCategoryTable a  where  a.MnemonicCode='{0}'", result);
                    getProductType = factoryMes.GetProductTypes.FromSqlRaw(SQL).ToList();
                }
            }
            return getProductType;
        }


        public static string GetGuaranteeInfo(string time) //DateTime time
        {
            // 按出库日期
            //string Guarantee = "保外";
            //var months = ((DateTime.Now.Year - time.Year) * 12) + DateTime.Now.Month - time.Month;
            //if (months > 18)
            //{
            //    Guarantee = "保外";
            //}
            //else
            //{
            //    Guarantee = "保内";
            //}

            // 按序列号判断是否保修的基础算法
            string guarantee = "保外";

            DateTime currentTime = DateTime.Now;
            int formattedTimeyear = Convert.ToInt32(currentTime.ToString("yyyy"));  // 当前年份
            int formattedTimemonth = Convert.ToInt32(currentTime.ToString("MM"));  // 当前月份
            int ye = 0;
            int y = 0; // 理论年份
            int z = 0; // 理论月份
            string input = time.Substring(0, 1);
            bool hasNonDigit = true;

            foreach (char c in input)
            {
                if (!Char.IsDigit(c))
                {
                    hasNonDigit = false;
                    break;
                }
            }
            // 处理流转吗情况，字母开头
            if (hasNonDigit)
            {
                string year = time.Substring(0, 2);   //序列号前两位   //年
                int date_dey = Convert.ToInt32(time.Substring(2, 2)) * 7;  //序列号2~4位   //日
                string[] yearr = { "16", "17", "18", "0U", "0V", "0W", "0X", "0Y", "0Z", "1A", "1B", "1C", "1D", "1E", "1F", "1G", "1H", "1I" };  //编码方式
                int[] yearrs = { 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2032, 2033 };  //序列号对应年份
                for (int i = 0; i < yearr.Length; i++)
                {
                    bool isEqual = string.Equals(year, yearr[i], StringComparison.OrdinalIgnoreCase);
                    if (isEqual)
                    {
                        ye = yearrs[i];
                        break;
                    }
                };
                int[] monthDays = { 31, ye % 4 == 0 && ye % 100 != 0 || ye % 400 == 0 ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  // 当年月份天数组成

                int days = 0;
                int month = 0;
                for (int i = 0; i < monthDays.Length; i++)
                {
                    days += monthDays[i];
                    if (date_dey <= days)
                    {
                        month = i + 1;
                        break;
                    }
                };

                if (formattedTimemonth >= 7)
                {
                    y = formattedTimeyear - 1;
                    z = formattedTimemonth - 6;
                }
                else
                {
                    y = formattedTimeyear - 2;   
                    z = formattedTimemonth + 6;  
                }


                if (y > ye)
                {
                    guarantee = "保外";
                }
                else if (y < ye)
                {
                    guarantee = "保内";
                }
                else
                {
                    if (z <= month)
                    {
                        guarantee = "保内";
                    }
                    else
                    {
                        guarantee = "保外";
                    }
                }
            }
            else
            {
                guarantee = "保外";
            }



            // string yearx = ye.ToString();  //输出年份
            // string months = month.ToString();   // 输出月份

            
            return guarantee;
        }

        public static string GetProductCategory(string ProductName)
        {
            string category = string.Empty;
            if (!string.IsNullOrEmpty(ProductName))
            {
                if (ProductName.Contains("驱动"))
                {
                    category = "驱动";
                }
                else if (ProductName.Contains("电机"))
                {
                    category = "电机";
                }
                else
                {
                    category = "数字";
                }
            }
            return category;
        }
    }
}
